package com.mimosa.common.view;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mimosa.common.customer.TCustomer;
import com.mimosa.common.customer.TCustomerAddress;
import com.mimosa.common.customer.TCustomerContact;

/**
 * 数据展现的帮助类
 * 
 * @author ggeagle
 * 
 */
public final class DataViewHelper {
	/**
	 * 日志记录器
	 */
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	/**
	 * 单实例
	 */
	static final DataViewHelper INS = new DataViewHelper();
	/**
	 * 视图展现的容器类
	 */
	private Map<String, String[]> viewContext = null;
	static final String ID = "id";
	static final String NAME = "name";
	static final String CODE = "code";
	static final String REMARK = "remark";
	static final String CREATE_TIME = "createTime";
	static final String UPDATE_TIME = "lastUpdateTime";

	/**
	 * 单实例对象
	 */
	private DataViewHelper() {
		this.viewContext = new HashMap<String, String[]>();
		// 获取对象的配置信息
		this.viewContext
				.put(TCustomer.class.getSimpleName().toLowerCase(),
						new String[] { ID, NAME, CODE, REMARK, CREATE_TIME,
								UPDATE_TIME });
		this.viewContext.put(TCustomerAddress.class.getSimpleName()
				.toLowerCase(), new String[] { ID, NAME, CODE, REMARK,
				CREATE_TIME, UPDATE_TIME });
		this.viewContext.put(TCustomerContact.class.getSimpleName()
				.toLowerCase(), new String[] { ID, NAME, CODE, REMARK,
				CREATE_TIME, UPDATE_TIME });
		// 获取自定义的配置信息
		Properties prop = new Properties();
		try {
			prop.loadFromXML(this.getClass().getResourceAsStream(
					"/config/dataview.xml"));
			for (Entry<Object, Object> entry : prop.entrySet()) {
				this.viewContext.put(entry.getKey().toString().toLowerCase(),
						entry.getValue().toString().split("#"));
			}
		} catch (IOException e) {
			logger.error(
					"cannot get config info from classpath : config/dataview.xml",
					e);
			e.printStackTrace();
		}
	}

	String[] getEntityNames(String entityName) {
		return viewContext.get(entityName.toLowerCase());
	}
}